from common.api import http_result
from common.utils.session import get_redis_conn
from developer_module.rpc import developer_pb2_grpc


class RedisHandler(developer_pb2_grpc.RedisServicer):

    def do_query_all_redis(self, param, session):
        redis = get_redis_conn()
        keys = redis.keys()
        result = []
        for k in keys:
            try:
                key = k.decode()
                key = str(key)
                if key.find('__session_') == 0:
                    continue
                result.append({
                    'key': key,
                    'value': str(redis.get(key).decode('unicode_escape')),
                })
            except:
                pass

        return self.resp(1, http_result.RESULT_SUCCESS, result)

    def do_delete_key(self, param, session):
        key = param.get('key')
        if not key:
            return self.need_param('key')
        get_redis_conn().delete(key)
        return self.resp(1, http_result.RESULT_SUCCESS)
